﻿<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    void Page_Load(Object o, EventArgs e)
    {
    }

//      Order No#OrderNo#text#1,Order Date#OrderDate#date#1
//      Order Note#OrderNote#memo#1,Order Status#OrderStatus#pick#1
//      t d p n c m
//  t-text
    //  d-date
    // p-pick
    //n-numbner
    // c- chkbox
    // m -memo
    // b -blank
    
    // pending
    // f Photo
    // l line beak
    // pending
    // retrieve from database and write to sql table then ui to browse table
    // also generate the schema from database
    
    
    
    void Edit_Click(Object o, EventArgs e)
    {
        //txtLayout.InnerText = "Editor";
        string objN = obj.Value;
        string colN =cols.Value;
        int coln = int.Parse(colN);
        string fieldS = fields.Value;
        string uiS = ui.Value;
        string otherS = other.Value;
        StringBuilder sb = new StringBuilder();
        StringBuilder sbDS = new StringBuilder();
        StringBuilder sbPV = new StringBuilder();
        sb.Append("\n<table>");
        sbPV.Append("\n<table>");
        fieldS = fieldS.Replace("\n", "");
        string[] sa0 = fieldS.Split(new char[] { '\r' });
        int tab =0;
        
        for(int z=0; z < sa0.Length; z++)
        {
            string[] sa = sa0[z].Split(new char[] { ',' });
            if (sa.Length != 3) continue;
            sb.Append("\n<tr>");
            sbPV.Append("\n<tr>");
            for (int i = 0; i < sa.Length; i++)
            {
                
                char typ = sa[i][0];
                // t,d,p,c,m,n  b-blank
                string st = sa[i].Substring(1, sa[i].Length - 1);
                switch (typ)
                {
                    case 't':
                        sb.AppendFormat("\n{0}",GenerateText(st,st,false,tab));
                        break;
                    case 'm':
                        sb.AppendFormat("\n{0}", GenerateMemo(st,st,false,tab,2));
                        break;
                    case 'd':
                        sb.AppendFormat("\n{0}", GenerateDate(st,st,false,tab, "MMM dd, yyyy"));
                        break;
                    case 'p':
                        sb.AppendFormat("\n{0}", GeneratePick(st,st,false,tab));
                        sbDS.AppendFormat("\n{0}", GenerateDS(st));
                        break;
                    case 'c':
                        sb.AppendFormat("\n{0}", GenerateCheck(st,st,false,tab));
                        break;
                    case 'n':
                        sb.AppendFormat("\n{0}", GenerateNumber(st,st,false,tab));
                        break;
                    case 'b':
                        sb.AppendFormat("\n{0}", GenerateBlank());
                        break;
                }
                switch (typ)
                {
                    case 't':
                        sbPV.AppendFormat("\n{0}", GenerateTextView(st, st, false, tab));
                        break;
                    case 'm':
                        sbPV.AppendFormat("\n{0}", GenerateMemoView(st, st, false, tab, 2));
                        break;
                    case 'd':
                        sbPV.AppendFormat("\n{0}", GenerateDateView(st, st, false, tab, "MMM dd, yyyy"));
                        break;
                    case 'p':
                        sbPV.AppendFormat("\n{0}", GeneratePickView(st, st, false, tab));
                        break;
                    case 'c':
                        sbPV.AppendFormat("\n{0}", GenerateCheckView(st, st, false, tab));
                        break;
                    case 'n':
                        sbPV.AppendFormat("\n{0}", GenerateNumberView(st, st, false, tab));
                        break;
                    case 'b':
                        sbPV.AppendFormat("\n{0}", GenerateBlankView());
                        break;
                }
            }
            sb.Append("\n</tr>");
            sbPV.Append("\n</tr>");
        }

        sb.Append("\n</table>");
        sbPV.Append("\n</table>");
        
        txtLayout.InnerText = sb.ToString();
        txtCode.InnerText = sbDS.ToString();
        txtView.InnerText = sbPV.ToString();
    }

    void List_Click(Object o, EventArgs e)
    {
        txtLayout.InnerText = "List";
    }

    void View_Click(Object o, EventArgs e)
    {
        txtLayout.InnerText = "View";
    }

    void Save_Click(Object o, EventArgs e)
    {
        // generate a file obj_type_date_time
    }

    string GenerateText(string label, string field, bool read, int tab)
    {
        string format = "<td class=\"tdLabel\">{0}</td><td class=\"tdInput\"><dxe:ASPxTextBox ID=\"text_{1}\" ClientInstanceName=\"text_{1}\" Readonly=\"{2}\" cssClass=\"txtInput\" Text='<%# Bind(\"{1}\") %>' TabIndex=\"{3}\" runat=\"server\" ></dxe:ASPxTextBox></td>";
        return String.Format(format, label, field, read, tab);
    }

    string GenerateDate(string label, string field, bool read, int tab, string dateFormat)
    {
        string format = "<td class=\"tdLabel\">{0}</td><td class=\"tdInput\"><dxe:ASPxDateEdit ID=\"date_{1}\" ClientInstanceName=\"date_{1}\" Readonly=\"{2}\" cssClass=\"txtInput\" value=\'<%# Bind(\"{1}\") %>\' EditFormat=\"Custom\" EditFormatString=\"{4}\" DisplayFormatString=\"{4}\" TabIndex=\"{3}\" runat=\"server\" ></dxe:ASPxDateEdit></td>";
        return String.Format(format, label, field, read, tab, dateFormat);
    }

    string GenerateMemo(string label, string field, bool read, int tab, int rows)
    {
        string format = "<td class=\"tdLabel\">{0}</td><td class=\"tdInput\"><dxe:ASPxMemo ID=\"memo_{1}\" ClientInstanceName=\"memo_{1}\" cssClass=\"txtMemo\" Rows=\"{4}\" Text='<%# Bind(\"{1}\") %>' TabIndex=\"{3}\" runat=\"server\"></dxe:ASPxMemo></td>";
        return String.Format(format, label, field, read, tab, rows);
    }

    string GeneratePick(string label, string field, bool read, int tab)
    {
        string format = "<td class=\"tdLabel\">{0}</td><td class=\"tdInput\"><dxe:ASPxComboBox ID=\"pick_{1}\" ClientInstanceName=\"pick_{1}\" Readonly=\"{2}\" cssClass=\"txtCombo\" Text='<%# Bind(\"{1}\") %>' DataSourceID=\"dsPick{1}\" ValueField=\"Id\" TextField=\"Code\" TabIndex=\"{3}\" runat=\"server\" ></dxe:ASPxComboBox></td>";
        return String.Format(format, label, field, read, tab);
    }

    string GenerateCheck(string label, string field, bool read, int tab)
    {
        string format = "<td class=\"tdLabel\">{0}</td><td class=\"tdInput\"><dxe:ASPxCheckBox ID=\"cbox_{1}\" ClientInstanceName=\"xbox_{1}\" Readonly=\"{2}\" cssClass=\"txtCombo\" Text='<%# Bind(\"{1}\") %>' DataSourceID=\"dsPick{1}\" ValueField=\"Id\" TextField=\"Code\" TabIndex=\"{3}\" runat=\"server\" ></dxe:ASPxCheckBox></td>";
        return String.Format(format, label, field, read, tab);
    }

    string GenerateNumber(string label, string field, bool read, int tab)
    {
        string format = "<td class=\"tdLabel\">{0}</td><td class=\"tdInput\"><dxe:ASPxSpinEdit ID=\"num_{1}\" ClientInstanceName=\"num_{1}\" Readonly=\"{2}\" cssClass=\"txtCombo\" Text='<%# Bind(\"{1}\") %>' DataSourceID=\"dsPick{1}\" ValueField=\"Id\" TextField=\"Code\" TabIndex=\"{3}\" runat=\"server\" ></dxe:ASPxSpinEdit></td>";
        return String.Format(format, label, field, read, tab);
    }

    string GenerateBlank()
    {
        string format = "<td class=\"tdLabel\">&nbsp;</td><td class=\"tdInput\">&nbsp;</td>";
        return String.Format(format, "");
    }


    string GenerateTextView(string label, string field, bool read, int tab)
    {
        string format = "<td class=\"tdLabel\">{0}</td><td class=\"tdPreview\"><%# Eval(\"{1}\") %></td>";
        return String.Format(format, label, field, read, tab);
    }

    string GenerateDateView(string label, string field, bool read, int tab, string dateFormat)
    {
        string format = "<td class=\"tdLabel\">{0}</td><td class=\"tdPreview\"><%# Eval(\"{1}\") %></td>";
        return String.Format(format, label, field, read, tab, dateFormat);
    }

    string GenerateMemoView(string label, string field, bool read, int tab, int rows)
    {
        string format = "<td class=\"tdLabel\">{0}</td><td class=\"tdPreview\"><%# Eval(\"{1}\") %></td>";
        return String.Format(format, label, field, read, tab, rows);
    }

    string GeneratePickView(string label, string field, bool read, int tab)
    {
        string format = "<td class=\"tdLabel\">{0}</td><td class=\"tdPreview\"><%# Eval(\"{1}\") %></td>";
        return String.Format(format, label, field, read, tab);
    }

    string GenerateCheckView(string label, string field, bool read, int tab)
    {
        string format = "<td class=\"tdLabel\">{0}</td><td class=\"tdPreview\"><%# Eval(\"{1}\") %></td>";
        return String.Format(format, label, field, read, tab);
    }

    string GenerateNumberView(string label, string field, bool read, int tab)
    {
        string format = "<td class=\"tdLabel\">{0}</td><td class=\"tdPreview\"><%# Eval(\"{1}\") %></td>";
        return String.Format(format, label, field, read, tab);
    }

    string GenerateBlankView()
    {
        string format = "<td class=\"tdLabel\">&nbsp;</td><td class=\"tdPreView\">&nbsp;</td>";
        return String.Format(format, "");
    }

    
    
    string GenerateDS(string pick)
    {
        string format = "<asp:SqlDataSource ID=\"ds{0}\" runat=\"server\" ConnectionString=\"<%$ ConnectionStrings:MSSQL %>\" SelectCommand=\"SELECT ddItemID as id,  ddItemCode as code FROM ddItems\" />";
        return String.Format(format, pick);
    }
        
//    <td class="tdLabel">Order No</td><td class="tdInput"><dxe:ASPxTextBox ID="text_OrderNo" ClientInstanceName="text_OrderNo" Readonly="True" cssClass="txtInput" Text='<%# Bind("JobNo") %>' TabIndex="7" runat="server" ></dxe:ASPxTextBox></td>
//    <td class="tdLabel">Service Date</td><td class="tdInput"><dxe:ASPxDateEdit ID="date_ServiceDate" ClientInstanceName="date_ServiceDate" Readonly="True" cssClass="txtInput" value="<%# Bind("JobDate") %>" EditFormat="Custom" EditFormatString="dd/MM/yyyy" DisplayFormatString="dd/MM/yyyy" TabIndex="7" runat="server" ></dxe:ASPxDateEdit></td>
//    <td class="tdLabel">Marketing Source</td><td class="tdInput"><dxe:ASPxComboBox ID="pick_MarketingSource" ClientInstanceName="pick_MarketingSource" Readonly="True" cssClass="txtCombo" Text='<%# Bind("MarketingSource") %>' DataSourceID="dsPickSource" ValueField="Id" TextField="Code" TabIndex="7" runat="server" ></dxe:ASPxComboBox></td>
//    <td class="tdLabel">Remarks</td><td class="tdInput"><dxe:ASPxMemo ID="txt_Remark" ClientInstanceName="text_Remark" cssClass="txtMemo" Rows="2" Text='<%# Eval("Remark") %>' TabIndex="4" runat="server"></dxe:ASPxMemo></td>

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <table width=800>
        <tr>
        <td width=100>Object Name</td>
        <td width=120><input name="obj" id="obj" type="text" size="20" runat="server" value="_base" /></td>
        <td width=100>No Of Columns</td>
        <td width=80><input name="cols" id="cols" type="text" size="10" runat="server" value="0" /></td>
        <td width=400 align=right>
            <asp:Button id="btnEdit" onclick="Edit_Click" runat="server" text="Gen Editor"></asp:Button>
            &nbsp;
            <asp:Button id="btnList" onclick="List_Click" runat="server" text="Gen List"></asp:Button>
            &nbsp;
            <asp:Button id="btnView" onclick="View_Click" runat="server" text="Gen View"></asp:Button>
            &nbsp;
            <asp:Button id="btnSave" onclick="Save_Click" runat="server" text="Save"></asp:Button>
        </td>
        </tr>
        <tr>
        <td>Fields Setup</td>
        <td width=700 colspan=5><textarea name="fields" id="fields" rows="3" cols="80" runat="server" ></textarea></td>
        </tr>
        <tr>
        <td>UI Setup</td>
        <td width=700 colspan=5><textarea name="ui" id="ui" rows="3" cols="80" runat="server" ></textarea></td>
        </tr>
        <tr>
        <td>Other Setup</td>
        <td width=700 colspan=5><textarea name="other" id="other" rows="3" cols="80" runat="server" ></textarea></td>
        </tr>
        <tr>
        <td colspan=6><hr /></td>
        </tr>
        <tr>
        <td>Layout</td>
        <td width=700 colspan=5><textarea name="txtLayout" id="txtLayout" rows="6" cols="80" runat="server" ></textarea></td>
        </tr>
        <tr>
        <td>Preview</td>
        <td width=700 colspan=5><textarea name="txtView" id="txtView" rows="6" cols="80" runat="server" ></textarea></td>
        </tr>
        <tr>
        <td>Code</td>
        <td width=700 colspan=5><textarea name="txtCode" id="txtCode" rows="6" cols="80" runat="server" ></textarea></td>
        </tr>
    </table>


    </div>
    </form>
</body>
</html>
